package com.d3.pomodoro.model

import android.text.TextUtils
import kotlin.collections.ArrayList

class Potato : BaseModel() {
    var title: String = ""     //标题
    var degreeType: Int = 0     //紧急程度 0无， 1低， 2中 ， 3高
    var finishType: Int = 0     //完成状态//0为进行中，1为已完成，3为休息中。  2不存在，历史问题
    var usedTomato: Int = 0       //总共用了几个番茄, 每个番茄完成时更新此字段
    var usedTime: Int = 0       //总共用了多久, 每个番茄完成时更新此字段
    var priority: Double = 0.0       //排序
    var goal: Int = 0        //总目标,正数为次数，负数为时间单位秒
    var everyGoal: Int = 0   //每日目标,正数为次数，负数为时间单位秒
    var cateId: String = ""       //分组，所属清单
    var remark: String = ""   //备注
    var beginTime: Long = 0   //提醒时间
    var deadlineStartTime: Long = 0 //时间开始,重复为10时有这个
        set(value) = if (value != 0L && repeatType == RepeatType.NONE) {
            repeatString = "1、2、3、4、5、6、7"
            field = value
        } else {
            field = value
        }

    var deadlineTime: Long = 0  //截止时间
        set(value) = if (value == 0L) {
            deadlineStartTime = 0
            field = value
        } else {
            field = value
        }
    //重复类型string， 1、2、3、4，5、6、7代表天， 8是月，9是年，重复的需要每日刷新，10是自定义重复。使用、隔开
    var repeatString: String = ""
    var duration: Int = 0    //每次做多长时间. (秒）
    var restDuration: Int = 0    //每次休息多长时间. (秒）,为-1则代表不需要休息
    var hide: Int = 0    //是否隐藏。到回收站（完成的隐藏了也会统计，回收站功能暂不开放，但是字段需要保留）
    var noticePlus: Int = 0 //增强提醒
    var gift: String = "" //奖励
    var focus: Int = -1 //专注, -1为没设置
    var forward: Int = -1 //正向， -1为没设置
    var resetSub: Int = -1 //重置子任务， -1为没设置
    var showInCalendar: Int = -1 //显示在日历,-1为没设置
    var pic: String = "" //附件的URL, 网络地址
    var finishedTimeString: String = "" //完成的时间戳集合，中文,隔开
    var colorString: String = "" //颜色
    var _localPic: String = "" //本地附件
    var _showFinishSub: Int = 1 //是否显示已完成子任务
    var _showMainSub: Int = 1 //首页是否展开子任务
    var _showMinTime: Long = 0 //小于这个时间不显示在今日、明日

    var __cate: Cate? = null
        get() {
            if (!TextUtils.isEmpty(cateId)) {
                //根据cateId从数据库获取对应的cate
//                return CateDao.sharedInstance.fetchCateById(cateId: "\(cateId)")
                return null
            }
            return null
        }

    var subAllPotatos: ArrayList<PotatoSub> = ArrayList()
        get() {
            //根据pid从数据库获取
//            PotatoSubDao.sharedInstance.fetchPhotoByParentId(parentId: pid)
            return ArrayList()
        }
    var subPotatosOnlyDoing: ArrayList<PotatoSub> = ArrayList()
        get() {
            //根据 pid和finishType:.Doing 获取
//            PotatoSubDao.sharedInstance.fetchPhotoByParentId(parentId: pid,finishType:.Doing)
            return ArrayList()
        }
    var subPotatos: ArrayList<PotatoSub> = ArrayList()
        get() {
            return ArrayList()
        }
    var todayCount: Int = 0
        get() {
            if (everyGoal != 0) {
//                return TomatoDao.sharedInstance.fetchPotatoTodayCount(potatoId: pid)
                return 0
            } else {
                return 0
            }
        }
    var todayCountRate: Double = 0.0
        get() {
            return 1.toDouble().coerceAtMost(todayCount.toDouble() / everyGoal.toDouble()) * 100

        }
    var countRate: Double = 0.0
        get() {
            return 1.toDouble().coerceAtMost(usedTomato.toDouble() / goal.toDouble()) * 100
        }
    var todayTime: Int = 0
        get() {
            if (everyGoal != 0) {
//                return TomatoDao.sharedInstance.fetchPotatoTodayTime(potatoId: pid)
            }
            return 0
        }
    var todayTimeRate: Double = 0.0
        get() {
            return 1.toDouble().coerceAtMost(todayTime.toDouble() / (-everyGoal).toDouble()) * 100
        }
    var timeRate: Double = 0.0
        get() {
            return 1.toDouble().coerceAtMost(usedTime.toDouble() / (-goal).toDouble()) * 100
        }
    var beginDate: Long = 0 //提醒时间date
    var updateDate: Long = 0//更新时间
    var createDate: Long = 0  //创建时间
    var degressType: Int = 0 //   0 NONE,1 LOW, 2 MEDIUM,3 HIGH
    var isFocus: Boolean = false
        get() {
            if (focus == -1) {
                return false
            } else if (focus == 0) {
                return false
            } else {
                return true
            }
        }
    var isForward: Boolean = false
        get() {
            if (forward == -1) {
                return field
            } else if (forward == 0) {
                return false
            } else {
                return true
            }
        }
    var isResetSub: Boolean = false
        get() {
            if (resetSub == -1) {
                return field
            } else if (resetSub == 0) {
                return false
            } else {
                return true
            }
        }
    var isShowInCalendar: Boolean = false
        get() {
            if (showInCalendar == -1) {
                return field
            } else if (showInCalendar == 0) {
                return false
            } else {
                return true
            }
        }
    var canEdit: Boolean = false
        get() {
            if (finishType != 1 && hide != 1 && isDelete != 1) {
                return true
            }
            return false

        }
    var finishedTime: ArrayList<Long> = ArrayList()
        get() {
            val result = ArrayList<Long>()
            val finishedTimeArr = finishedTimeString.split(",")
            for (index in finishedTimeArr) {
                result.add(index.toLong())
            }

            return result

        }
    var lastFinishedTime: Long = 0
        get() {
            if (field == finishedTime.last()) {
                return field
            }
            return updateDate
        }

    var finishedTimeStrings: ArrayList<String> = ArrayList()
        get() {
            val result = ArrayList<String>()
            val fitter = finishedTimeString.split(",")
            for (index in fitter) {
                result.add(index)
            }
            return result
        }
    //0..6 ，8是月，9是年 正常的weekday是1..7需要注意转换. 日、一、二、三、四、五
    var repeatTime: ArrayList<Int> = ArrayList()
        get() {
            val result = ArrayList<Int>()
            if (repeatString == "8" || repeatString == "9" || repeatString == "" || repeatString == "10") { //避免循环调用，别使用repeatType
                return result
            }

            val arr = repeatString.split("、")
            for (index in arr) {
                result.add(index.toInt())

            }
            return result
        }
    var repeatType: RepeatType = RepeatType.NONE
        get() {
            if (repeatString == "") {
                return RepeatType.NONE
            } else if (repeatString == "8") {
                return RepeatType.MONTH
            } else if (repeatString == "9") {
                return RepeatType.YEAR
            } else if (repeatString == "10") {
                return RepeatType.CUSTOM
            } else {
                if (repeatTime.size == 7) {
                    return RepeatType.DAY
                }
                return RepeatType.WEEK
            }
        }
    var nextRemindDay: Long = 0  //下一次的提醒时间
        get() {
            val result = 0L

            val nowTime = System.currentTimeMillis()
            if (beginTime == 0L) {
                return 0
            }

            if (repeatType == RepeatType.NONE) {
                if (beginTime > nowTime) { //已过期的就不用了
                    return beginDate
                }
            } else {
                if (nowTime < deadlineStartTime) {  //有设置开始时间
                    return deadlineStartTime
                }

                if (repeatType == RepeatType.MONTH) {

                }
            }

            return result
        }
    var nextRemindTime: Int = 0
    var deadlineDate: Long = 0
    var deadlineStartDate: Long = 0
    var isToday: Boolean = false
    var isTomorrow: Boolean = false
    //提醒时间 + 加强
    var noticeShowString: String = ""
    //提醒时间 + 加强 + 做多久
    var noticeAndDurationShowString: String = ""
    //仅显示剩余
    var deadlineShowRemainString: String = ""
    //未逾期 12/31 周一 还剩xx天
    //已逾期 已逾期1
    var deadlineShowString: String = ""
    var repeatShowString: String = ""
    var remainDay: Int = 0   //距离截止日期还有几天
    var showDurationString: String = "" //每次做多久

}